﻿namespace Algorithm;

public class RangeExtraction
{
	/// <summary>
	/// 获取数字区间，如果区间中数字大于2个，用-连接，否则用,连接
	/// </summary>
	/// <param name="args"></param>
	/// <returns></returns>
	public static string Extract(int[] args)
	{
		if (args.Length == 1)
		{
			return args[0].ToString();
		}
		List<string> list = [];
		List<string> sub = [];
		List<string> range = [];
		for (int i = 0; i < args.Length; i++)
		{
			sub.Add(args[i].ToString());
			if (i == args.Length - 1 || args[i] + 1 != args[i + 1])
			{
				if (sub.Count == 1)
				{
					range.Add(sub[0]);
				}
				else
				{
					range.Add(sub[0]);
					range.Add(sub[sub.Count - 1]);
				}
				list.Add(string.Join(sub.Count > 2 ? '-' : ',', range));
				sub.Clear();
				range.Clear();
			}
		}
		return string.Join(',', list);
	}
}
